package com.hdc.community.util;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
public class StringUtil {

	//生成随机字符串
	public static  String generateUuid(){
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	//MD5加密
	//string+随机字符串

	public static  String md5(String key){
		if(StringUtils.isBlank(key)){
			return null;
		}
		return DigestUtils.md5DigestAsHex(key.getBytes());
	}

	public static  String getJsonString(int code ,String msg,Map<String ,Object> map){
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", code);
		jsonObject.put("msg",msg);
		if( map != null){
			for (String key :
					map.keySet()) {
				jsonObject.put(key, map.get(key));
			}
		}
		return jsonObject.toJSONString();
	}

	public static String getJsonString(int code ,String msg){

		return getJsonString(code, msg, null);
	}

	public static String getJsonString(int code){

		return getJsonString(code, null, null);
	}

	public static void main(String[] args) {

		Map<String,Object> map = new HashMap<>();

		map.put("name", "张三");
		map.put("age", 25);
		String jsonString = getJsonString(200, "hahahah", map);
		System.out.println(jsonString);

	}
}
